package org.bwf.study;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.beanutils.BeanUtils;
import org.bwf.study.mapper.CategoryMapper;
import org.bwf.study.mapper.CommentMapper;
import org.bwf.study.mapper.FilmMapper;
import org.bwf.study.mapper.FilmRegionMapper;
import org.bwf.study.model.Category;
import org.bwf.study.model.Comment;
import org.bwf.study.model.Film;
import org.bwf.study.model.FilmRegion;
import org.bwf.study.util.ElasticSearchClientUtil;
import org.bwf.study.util.JsonUtil;
import org.elasticsearch.index.query.BoolQueryBuilder;
import org.elasticsearch.index.query.MatchQueryBuilder;
import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.index.query.TermQueryBuilder;
import org.elasticsearch.search.SearchHit;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;

import javax.annotation.Resource;
import java.lang.reflect.InvocationTargetException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@SpringBootTest(classes = CatEyeApplication.class)
@RunWith(SpringRunner.class)
@Slf4j
public class CatEyeESTestApplication {

    @Resource
    private ElasticSearchClientUtil esUtil;

    @Autowired
    private CategoryMapper categoryMapper;
    @Test
    public void AddCategoryTest(){
        List<Category> categoryList = categoryMapper.selectList(null);
        esUtil.bulkAddDocument("category", categoryList);
    }

    @Autowired
    private FilmRegionMapper filmRegionMapper;
    @Test
    public void AddRegionTest(){
        List<FilmRegion> filmRegionList = filmRegionMapper.selectList(null);
        esUtil.bulkAddDocument("region", filmRegionList);
    }

    @Autowired
    private FilmMapper filmMapper;
    @Test
    public void AddFilmTest(){
        List<Film>  filmList = filmMapper.selectList(null);
        esUtil.bulkAddDocument("film", filmList);
    }

    @Autowired
    private CommentMapper commentMapper;
    @Test
    public void AddCommentTest(){
        List<Comment> commentList = commentMapper.selectList(null);
        esUtil.bulkAddDocument("comment", commentList);
    }

    @Test
    public void ESQueryTest(){
        // SearchHit是ES中的结果对象，包含了所有es需要返回的信息
//        SearchHit[] filmHits = esUtil.MatchPhraseQueryDocument("film","filmName","指环王",10,0);
//        SearchHit[] filmHits = esUtil.matchQueryDocument("film","filmName","指环王",10,0);
        // 精确和模糊的查询对象，都使用QueryBuilders工厂进行创建
//        TermQueryBuilder term = QueryBuilders.termQuery("filmName","指环王");
//        MatchQueryBuilder match = QueryBuilders.matchQuery("filmName", "指环王");
        // 条件查询对象  所有的查询对象的总父类就是 QueryBuilder
        BoolQueryBuilder boolQueryBuilder = QueryBuilders.boolQuery();
        boolQueryBuilder.filter(QueryBuilders.matchQuery("filmName", "指环王"));
        boolQueryBuilder.filter(QueryBuilders.termQuery("filmYears", "2021"));
        SearchHit[] filmHits = esUtil.boolQueryDocuments("film",10,0,boolQueryBuilder);
        List<Film> filmList = new ArrayList<>();
        for(var s : filmHits){
//            Film film = JsonUtil.decodeJson(s.getSourceAsString(), Film.class);
            Film film = new Film();

            try {
                BeanUtils.populate(film, s.getSourceAsMap());
                filmList.add(film);
            } catch (IllegalAccessException | InvocationTargetException e) {
                e.printStackTrace();
            }

//            log.(s.getSourceAsMap());
        }



    }
}
